﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace wrsm.common.utility {

    /// <summary>
    /// Provides utility File System Function
    /// </summary>
    [CLSCompliant(true)]
    public static class FSUtility {

        /// <summary>
        /// Create specified path
        /// </summary>
        /// <param name="path">
        /// Path to create
        /// </param>
        /// <remarks>
        /// this will attempt to create all directories needed to satisfy the given path
        /// </remarks>
        public static bool CreateDirectoryPath(string path) {
            //TODO: Look at this, it's not creating the directory structure once deployed.....

            if (Directory.Exists(path)) //Don't bother doing this is the path already exists
                return true;
            string[] strTok = path.Split(new char[] { '\\', '/' }, StringSplitOptions.RemoveEmptyEntries);
            string combined = string.Empty;
            try {
                foreach (string part in strTok) {
                    combined = Path.Combine(combined, part);
                    if (!Directory.Exists(combined)) {
                        Directory.CreateDirectory(combined);
                    }
                }
            } catch (Exception) {
                //If this fails, we are unable to create the directory structure.  This is noramlly because of permissiosn,
                //since we are checking if each section of the directory already exists
                return false;
            }
            return Directory.Exists(combined);
        }

    }
}
